
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class AttachFile
    {
         #region Common
        public eAttachFile   Data;

        public AttachFile()
        {
            Data = new eAttachFile();
		}

        ~AttachFile()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cAttachFile objDAL = new cAttachFile();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cAttachFile objDAL = new cAttachFile();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cAttachFile objInfor = new cAttachFile(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.AttachFileID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cAttachFile objInfor = new cAttachFile();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateByProductID(int ID, int nProductID)
        {
            int nResult = 0;
            cAttachFile objDAL = new cAttachFile();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.ProductID = nProductID;

                objDAL.Save();
                nResult = objDAL.AttachFileID ;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cAttachFile objInfor)
        {
            objInfor.FileName = Data.FileName;
            objInfor.ProductID = Data.ProductID; 
            objInfor.Alias1 = Data.Alias1; 
            objInfor.Alias2 = Data.Alias2; 
            objInfor.Alias3 = Data.Alias3; 
            objInfor.CreatedDate = Data.CreatedDate; 
            objInfor.Status = Data.Status;
          
        }

        private void ConvertData(cAttachFile objInfor)
        {
            Data.FileName = objInfor.FileName;
            Data.ProductID = objInfor.ProductID;
            Data.Alias1 = objInfor.Alias1;
            Data.Alias2 = objInfor.Alias2;
            Data.Alias3 = objInfor.Alias3;
            Data.CreatedDate = objInfor.CreatedDate;
            Data.Status = objInfor.Status;

        }

        public DataTable Search(int ItemView,string Title,int ProductID, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cAttachFile objDAL = new cAttachFile();
            tbResult = objDAL.Search(ItemView, Title, ProductID, PageIndex, PageSize, ref  PageCount);
            return tbResult;
        }

        public DataTable GetByDate(int DateEffect)
        {
            DataTable tbResult = new DataTable();
            cAttachFile objDAL = new cAttachFile();
            tbResult = objDAL.GetByDate(DateEffect);
            return tbResult;
        }


        public DataTable DeleteByDateEffect(int DateEffect)
        {
            DataTable tbResult = new DataTable();
            cAttachFile objDAL = new cAttachFile();
            tbResult = objDAL.DeleteByDateEffect(DateEffect);
            return tbResult;
        }

        public bool CheckExistPage(int DateEffect)
        {
            bool isExist = false;
            cAttachFile objDAL = new cAttachFile();
            isExist = objDAL.CheckExistDateEffect(DateEffect);
            return isExist;
        }
        #endregion
    }
}
